import constants from "./constants"

// 创建的要存储的初始化state
const initialState = {
    name: "chenyq",
    age: 18,
    token: localStorage.getItem('token_key') || ""
}

// 定义reducer, 将要存储的state作为返回值返回
// 第一次state是undefined, 因此给一个默认值将初始化数据添加到store中
function reducer(state = initialState, action) {
    switch (action.type) {
        case constants.CHANGE_NAME:
            return { ...state, name: action.name }
        case constants.CHANGE_AGE:
            return { ...state, age: state.age + action.num }
        case constants.DENGLU:
            {
                // 更新state中的token
                return { ...state, token: action.token }
            }
    }

    // 没有数据更新时, 返回之前的state
    return state
}

export default reducer
